<template>
  <div >  	
  	<ul class="foodlist"> 		
  		<li class="foodbox" v-for="food in foods">			
	  		<router-link  :to="{ name: 'food',params:{id: food.id }}">
	      	<img :src="food.tpuri" class="food_img" @touchstart="toFood($event,food)"/>
	  		</router-link>
  		  <div class="foodtit">
  		  	<h4 class="food_name">{{food.cpmc}}</h4>
  		  	<em class="food_rmb">￥{{food.cpjg}}</em> 		  	
  		  		<div class="food_add" @click="addcart($event,food)"><i class="iconfont icon-jia"></i></div>  		  		
  		  </div>  		  
  		</li>
  	</ul>
  </div>
</template>
<style scoped src="../assets/css/foodlist.css"></style>
<script >
import axios from "axios"
export default { 
	props: ['foods'],
  data () {
    return {
     id:1,//路由地址显示
     cartfood:{},    
     cartfoods:[],
     totalprice:0,
     numbers:0,
     buynumber:1,
     goorderzt:sessionStorage["zt"],
    }    
  },
  mounted(){
  	
  	 	
  },
  methods:{   
	    toFood: function ($event,food){ 
	    	//console.log(food.id);	    	
			localStorage.cpid=food.id;			 
			 this.$emit('tofood');
	    },
	    addcart: function ($event,food){	
	    	sessionStorage["zt"]=1;
	    	axios.get('diancai?cpid='+food.id).then((r) => {	      		     
			      //console.log(r.data);
			      
			      		axios.get('yidiancaiping').then((res) => {	 
			      			//console.log(res.data);
			    				this.cartfoods=res.data;
			    				//console.log(this.cartfoods);
			    				this.numbers=0;
			    				this.totalprice=0;
			    				for (var i=0;i<this.cartfoods.length;i++){					
										var price=this.cartfoods[i].cpjg;
										var number=Number(this.cartfoods[i].ydfs);
										console.log(i);
										console.log(number);
										this.totalprice +=price*number;
										this.numbers+=number;
									}
							    	this.$emit('addcart',this.cartfoods,this.totalprice,this.numbers,this.goorderzt);
		      			});	
		      });	
		   	}
	}
}
</script>



